import {
  getFlowTable,
  getAccountBalance
} from '../../api/financial'
import {
  COMMISSION_LIST,
  COMMISSION_ACCOUNT_LIST,
  ACCOUNT_BALANCE,
  COMMISSION_DATA_COUNT,
  COMMISSION_HISTORY_DATA,
  COMMISSION_PAGE_SIZE
} from '../motations-type'

export default {
  state: {
    commissionList: [],
    commissionAccountList: [],
    accountBalance: [],
    commissionDataCount: null,
    commissionPageSize: 20,
    commissionHistoryData: [],
    row: {
      ctime: '',
      type: '',
      type0: '',
      money: '',
      des: ''
    }
  },
  getters: {
    commissionList: state => state.commissionList
  },
  mutations: {
    [COMMISSION_LIST] (state, data) {
      state.commissionList = data
    },
    [COMMISSION_ACCOUNT_LIST] (state, data) {
      state.commissionAccountList = data
    },
    [COMMISSION_DATA_COUNT] (state, data) {
      state.commissionDataCount = data
    },
    [COMMISSION_HISTORY_DATA] (state, data) {
      state.commissionHistoryData = data
    },
    [COMMISSION_PAGE_SIZE] (state, data) {
      state.commissionPageSize = data
    },
    [ACCOUNT_BALANCE] (state, data) {
      state.accountBalance = data
    }
  },
  actions: {
    async getCommissionList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      let newSumData = []
      let overcommission = 0
      let sumCommissionAccount = 0
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getFlowTable(data)
      resp.data.data.rows.map((item) => {
        overcommission = 1000000
        newData = [...newData, {
          id: item.id,
          ctime: item.ctime,
          type: item.type,
          type0: item.type0,
          money: item.money,
          des: item.des
        }]
      })
      commit(COMMISSION_LIST, newData)
      commit(COMMISSION_ACCOUNT_LIST, newSumData)
      commit(COMMISSION_DATA_COUNT, resp.data.data.total)
      if (newData.length < rootState.commission.commissionPageSize) {
        commit(COMMISSION_HISTORY_DATA, newData)
      } else {
        commit(COMMISSION_HISTORY_DATA, newData.slice(0, rootState.commission.commissionPageSize))
      }
      return resp
    },
    async changeCommissionPage ({ commit, rootState }, index) {
      let data = rootState.commission.commissionList
      let _start = (index - 1) * rootState.commission.commissionPageSize
      let _end = index * rootState.commission.commissionPageSize
      commit(COMMISSION_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeCommissionPageSize ({ commit, rootState }, value) {
      commit(COMMISSION_PAGE_SIZE, value)
    },
    async getAccountBalanceData ({ commit, rootState }) {
      const resp = await getAccountBalance()
      commit(ACCOUNT_BALANCE, resp.data.data)
    }
  }
}
